package com.cares.business.javamethod;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

import com.cares.event.utils.DateUtil;

/**
 * 调用bat、sh文件类
 * @author xuji
 *
 */
public class CallCommandFile {

	/**
	 * 获取应用部署后所在的操作系统，只分为两类：Windows和unix（或linux）
	 * return: Windows系统返回true，否则false
	 * */
	public static boolean isWindows(){
		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name");
		
		if(os.startsWith("win") || os.startsWith("Win")){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Windows系统下：资源URL转换成file路径
	 * param:filePath-调用类文件的URI；类似：/D:/apache-tomcat-6.0.33/webapps/mukpi/WEB-INF/classes/
	 * 处理返回	当前应用部署后所在的服务路径；类似：D:\apache-tomcat-6.0.33\webapps\mukpi
	 * */
	public static String getAppPathOfWin(String filePath){
		//后去当前应用部署后的路径
		//String path = BalTransSegWindow.getClass().getClassLoader().getResource("").getPath();
		if(filePath.startsWith("/")){
			filePath = filePath.substring(1, filePath.length()-1);
		}
		filePath = filePath.substring(0, filePath.lastIndexOf("WEB-INF"));  
		String[] paths = filePath.split("/");
		//将路径拆分，重新组装成目录
		String pathTemp = "";
		for(String str : paths){
			if(!pathTemp.isEmpty()){
				pathTemp = pathTemp+File.separator+str;
			}else{
				pathTemp = str;
			}
		}
		return pathTemp;
	}
	
	/**
	 * param:filePath-调用类文件的URI；类似：/root/tomcat6/webapps/MUKPI/WEB-INF/classes
	 * 返回linux下，应用部署后的路径:类似：/root/tomcat6/webapps/MUKPI
	 * */
	public static String getAppPathOfLinux(String filePath){
		return  filePath.substring(0, filePath.lastIndexOf("/WEB-INF"));
	}

	/**
	 * path：当前应用部署后的路径
	 * batName：要调用的bat文件名
	 * num: 成功导入的数据行数
	 * */
	public static int executeBatFile(String batPath, String batName) {
		BufferedReader br = null;
		 int num = 0;
		    try {   
		    	
		    	String cmdStr = "cmd /c "+batPath+File.separator+batName+" "+batPath;

		    	Process p = Runtime.getRuntime().exec(cmdStr);
		        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		        p.getOutputStream().flush();
		        p.getOutputStream().close();
		        
		        String line = "";
		        num = 0;
		        //加载的数据行数：加载完成 - 逻辑记录计数 000000。
		        while ((line = br.readLine()) != null) {
		        	//System.out.println(line);
		        	if(line.startsWith("加载完成")){
		        		String[] ms = line.split(" ");
		        		num = Integer.parseInt((String)(ms[ms.length-1].subSequence(0, ms[ms.length-1].length()-1)));
		        	}
		        }
		        br.close();
		    } catch (IOException e) {
		    	e.printStackTrace();
		    }finally{
		    	try{
	    		   br.close();
	    	    }catch (IOException e) {
		           e.printStackTrace();
		        }
	       }
		   return num;
	 }
	
	/**
	 * shPath：当前应用部署后的路径
	 * shFileName：要调用的shell文件名
	 * d：开始导入数据的时间
	 * num: 成功导入的数据行数
	 * */
	public static int executeShFile(String shPath, String shFileName) {
		BufferedReader br = null;
		 int num = 0;
		    try {   
		    	String d = DateUtil.getDateStr(new Date(), DateUtil.DATE_FORMATE_FULL);
		    	String shStr = "/bin/sh "+shPath+File.separator+shFileName+" "+shPath+" "+d;

		    	Process p = Runtime.getRuntime().exec(shStr);
		        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		        p.getOutputStream().flush();
		        p.getOutputStream().close();
		        
		        String line = "";
		        num = 0;
		        //加载的数据行数：加载完成 - 逻辑记录计数 000000。
		        while ((line = br.readLine()) != null) {
		        	//System.out.println(line);
		        	if(line.startsWith("Load completed - logical record count")){
		        		String[] ms = line.split(" ");
		        		//num=ms.length;
		        		num = Integer.parseInt((String)(ms[ms.length-1].subSequence(0, ms[ms.length-1].length()-1)));
		        	}
		        }
		        br.close();
		    } catch (IOException e) {
		    	e.printStackTrace();
		    }finally{
		    	try{
	    		   br.close();
	    	    }catch (IOException e) {
		           e.printStackTrace();
		        }
	       }
		   return num;
	 }
	
	/**
	 * path：当前应用部署后的路径
	 * batName：要调用的bat文件名
	 * file_number：导入的文件数
	 * version：版本号
	 * T：指标类型
	 * num: 成功导入的数据行数
	 * */
	public static int executeBatFile(String batPath, String batName, String v, String t) {
		BufferedReader br = null;
		 int num = 0;
		    try {
		    	String cmdStr = "cmd /c "+batPath+File.separator+batName+" "+batPath 
		    	+ " " + v + " " + t;

		    	Process p = Runtime.getRuntime().exec(cmdStr);
		        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		        p.getOutputStream().flush();
		        p.getOutputStream().close();

		        String line = "";
		        num = 0;
		        //加载的数据行数：加载完成 - 逻辑记录计数 000000。
		        while ((line = br.readLine()) != null) {
		        	System.out.println(line);
		        	if(line.startsWith("达到保存数据点")){
		        		String[] ms = line.split(" ");
		        		num = Integer.parseInt((String)(ms[ms.length-1].subSequence(0, ms[ms.length-1].length()-1)));
		        	}
		        }
		        br.close();
		    } catch (IOException e) {
		    	e.printStackTrace();
		    }finally{
		    	try{
	    		   br.close();
	    	    }catch (IOException e) {
		           e.printStackTrace();
		        }
	       }
		   return num;
	 }
	
	/**
	 * shPath：当前应用部署后的路径
	 * shFileName：要调用的shell文件名
	 * file_number：导入的文件数
	 * version：版本号
	 * t:指标
	 * d：开始导入数据的时间
	 * num: 成功导入的数据行数
	 * */
	public static int executeShFile(String shPath, String shFileName, String v, String t) {
		BufferedReader br = null;
		 int num = 0;
		    try {
		    	String d = DateUtil.getDateStr(new Date(),DateUtil.DATE_FORMATE_FULL);
		    	String shStr = "/bin/sh "+shPath+File.separator+shFileName+" "+shPath 
		    	+ " " + v + " " + t+ " "+d;

		    	Process p = Runtime.getRuntime().exec(shStr);
		        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		        p.getOutputStream().flush();
		        p.getOutputStream().close();

		        String line = "";
		        num = 0;
		        //加载的数据行数：加载完成 - 逻辑记录计数 000000。
		        int i=0;
		        while ((line = br.readLine()) != null) {
		        	System.out.println(line);
		        	if(line.startsWith("达到保存数据点")){
		        		String[] ms = line.split(" ");
		        		num += Integer.parseInt((String)(ms[ms.length-1].subSequence(0, ms[ms.length-1].length()-1)));
		        	}
		        }
		        br.close();
		    } catch (IOException e) {
		    	e.printStackTrace();
		    }finally{
		    	try{
	    		   br.close();
	    	    }catch (IOException e) {
		           e.printStackTrace();
		        }
	       }
		   return num;
	 }
}

